package grape.workflow.rest.form.mapper;

import grape.workflow.rest.form.vo.DynamicFormTypeVo;
import grape.workflow.rest.form.vo.DynamicFormVo;
import grape.workflow.rest.form.vo.RelationFormVo;
import org.flowable.bpmn.model.FormValue;
import org.flowable.engine.form.FormProperty;
import org.flowable.engine.impl.persistence.entity.HistoricFormPropertyEntityImpl;
import org.flowable.form.model.FormField;
import org.flowable.rest.service.api.form.RestEnumFormProperty;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.ReportingPolicy;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yangwei
 * Created at 2020/7/1 19:49
 */
@Mapper(componentModel = "spring",unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface FormWebMapper {

    @Mappings({
        @Mapping(source = "required", target = "isRequired"),
        @Mapping(source = "readOnly", target = "isReadOnly"),
        @Mapping(source = "overrideId", target = "isOverrideId"),
    })
    RelationFormVo formFieldToVo(FormField formField);

    default DynamicFormVo formPropertyToVo(org.flowable.bpmn.model.FormProperty formProperty, HistoricFormPropertyEntityImpl formPropertyEntity) {
        if (formProperty == null) {
            return null;
        }
        DynamicFormVo dynamicFormVo = new DynamicFormVo();
        dynamicFormVo.setId(formProperty.getId());
        dynamicFormVo.setName(formProperty.getName());
        if (formPropertyEntity != null) {
            dynamicFormVo.setValue(formPropertyEntity.getPropertyValue());
        }
        dynamicFormVo.setIsWritable(formProperty.isWriteable());
        dynamicFormVo.setIsReadable(formProperty.isReadable());
        dynamicFormVo.setIsRequired(formProperty.isRequired());

        DynamicFormTypeVo dynamicFormTypeVo = new DynamicFormTypeVo();
        dynamicFormTypeVo.setName(formProperty.getType());
        if ("enum".equals(formProperty.getType())) {
            List<FormValue> values = formProperty.getFormValues();
            if (values != null && !values.isEmpty()) {
                Map map = new HashMap();
                for (FormValue value : values) {
                    map.put(value.getId(), value.getName());
                }
                dynamicFormTypeVo.setInformation(map);
            }
        } else if ("date".equals(formProperty.getType())) {
            dynamicFormTypeVo.setInformation(formProperty.getDatePattern());
        }

        dynamicFormVo.setType(dynamicFormTypeVo);
        return dynamicFormVo;
    }

    /**
     *
     * @param formProperty
     * @return
     */
    default DynamicFormVo formPropertyToVo(FormProperty formProperty) {
        if (formProperty == null) {
            return null;
        }
        DynamicFormVo dynamicFormVo = new DynamicFormVo();
        dynamicFormVo.setId(formProperty.getId());
        dynamicFormVo.setName(formProperty.getName());
        dynamicFormVo.setValue(formProperty.getValue());
        dynamicFormVo.setIsWritable(formProperty.isWritable());
        dynamicFormVo.setIsReadable(formProperty.isReadable());
        dynamicFormVo.setIsRequired(formProperty.isRequired());

        DynamicFormTypeVo dynamicFormTypeVo = new DynamicFormTypeVo();
        dynamicFormTypeVo.setName(formProperty.getType().getName());
        dynamicFormTypeVo.setOriginFormType(formProperty.getType());
        if ("enum".equals(formProperty.getType().getName())) {
            // debug 发现，values 是一个map，key为值，value为标签名
            Object values = formProperty.getType().getInformation("values");
            dynamicFormTypeVo.setInformation(values);
        } else if ("date".equals(formProperty.getType().getName())) {
            dynamicFormTypeVo.setInformation(formProperty.getType().getInformation("datePattern"));
        }else {
            dynamicFormTypeVo.setInformation(formProperty.getType().getInformation(formProperty.getId()));
        }

        dynamicFormVo.setType(dynamicFormTypeVo);
        return dynamicFormVo;
    }
}
